package com.lscdu.gulimall.ruoyi_springboot.utis.security.filter;

import com.lscdu.gulimall.ruoyi_springboot.bean.login.LoginUser;
import com.lscdu.gulimall.ruoyi_springboot.service.TokenService;
import com.lscdu.gulimall.ruoyi_springboot.utis.security.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 啊昌
 * @date 2024/9/10 11:11
 * JwtAuthenticationTokenFilter 拦截请求 Token认证
 */

@Component
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
	@Autowired
	private TokenService tokenService;

	@Override
	protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
		LoginUser loginUser = tokenService.getLoginUser(request);
		if(loginUser !=null && SecurityUtils.getAuthentication() != null){
			// 更新令牌的过期时间
			tokenService.verifyToken(loginUser);
			//验证
			UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(loginUser, loginUser.getAuthorities());

		}
		//放行
		filterChain.doFilter(request, response);
	}
}
